package com.color.dao.impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class HibernateBaseDao<T extends Serializable>{
	
	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
    private Session getSession(){
    	return this.sessionFactory.getCurrentSession();
    }
    
	/**
     * 新增或者修改
     * @param t
     * @return
     */
    public int save(T t){
    	int iRet = 0;
    	try{
    		getSession().saveOrUpdate(t);
    		getSession().flush();
    		iRet = 1;
    	}catch (Exception e) {
			e.printStackTrace();
		}
    	return iRet;
    }
    
    /**
     * 删除
     * @param t
     * @return
     */
    public int delete(T t){
    	int iRet = 0;
    	try{
	    	getSession().delete(t);
	    	getSession().flush();
	    	iRet = 1;
    	}catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return iRet;
    }
    
    
    /**
     * 数据表查询公共函数(SQL)
     * @param openSql
     * @return
     */
	public List<Object> openSql(String openSql){
		return openSql(openSql,null,null);
	}
    
    /**
     * 数据表查询公共函数(SQL)
     * @param openSql
     * @param map
     * @return
     */
	public List<Object> openSql(String openSql,Map<String, Object> map){
		return openSql(openSql,map,null);
	}
    
	/**
	 * 数据表查询公共函数(SQL)
	 * @param openSql
	 * @param objs
	 * @return
	 */
	public List<Object> openSql(String openSql,Object[] objs){
		return openSql(openSql,null,objs);
	}
	
    /**
     * 数据表查询公共函数(SQL)
     * @param openSql
     * @param map
     * @param objs
     * @return
     */
	@SuppressWarnings("unchecked")
	private List<Object> openSql(String openSql,Map<String, Object> map,Object[] objs){
    	List<Object> list = null;
        Query query = null;
        try{
        	query = getSession().createSQLQuery(openSql);
        	
        	setQueryParameter(query,map,objs);
            
        	list = query.list();
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return list;
    }
    
	/**
	 * 数据表查询公共函数(HQL)
	 * @param openHql
	 * @return
	 */
	public List<T> openHql(String openHql){
		return openHql(openHql,null,null);
	}
	
	/**
	 * 数据表查询公共函数(HQL)
	 * @param openHql
	 * @param map
	 * @return
	 */
	public List<T> openHql(String openHql,Map<String, Object> map){
		return openHql(openHql,map,null);
	}
    
	/**
	 * 数据表查询公共函数(HQL)
	 * @param openHql
	 * @param objs
	 * @return
	 */
	public List<T> openHql(String openHql,Object[] objs){
		return openHql(openHql,null,objs);
	}
    
    /**
     * 数据表查询公共函数(HQL)
     * @param openHql
     * @param map
     * @param objs
     * @return
     */
    @SuppressWarnings("unchecked")
	private List<T> openHql(String openHql,Map<String, Object>  map,Object[] objs){
    	List<T> list = null;
        Query query = null;
        try{
        	query = getSession().createQuery(openHql);
        	
        	setQueryParameter(query,map,objs);
            
        	list = query.list();
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return list;   
    }
    
    /**
     * 数据表查询分页公共函数(HQL)
     * @param openHql
     * @param page
     * @param size
     * @return
     */
	public List<T> openHqlPage(String openHql,int page,int size){
    	return openHqlPage(openHql,page,size,null,null);
    }
    
    /**
     * 数据表查询分页公共函数(HQL)
     * @param openHql
     * @param page
     * @param size
     * @param map
     * @return
     */
	public List<T> openHqlPage(String openHql,int page,int size,Map<String,Object> map){
    	return openHqlPage(openHql,page,size,map,null);
    }
    
    /**
     * 数据表查询分页公共函数(HQL)
     * @param openHql
     * @param page
     * @param size
     * @param objs
     * @return
     */
	public List<T> openHqlPage(String openHql,int page,int size,Object[] objs){
    	return openHqlPage(openHql,page,size,null,objs);
    }
    
    /**
     * 数据表查询分页公共函数(HQL)
     * @param openHql
     * @param page
     * @param size
     * @param map
     * @param objs
     * @return
     */
    @SuppressWarnings("unchecked")
	private List<T> openHqlPage(String openHql,int page,int size,Map<String,Object> map,Object[] objs){
    	List<T> list = null;
        Query query = null;
        try{
        	query = getSession().createQuery(openHql);
        	
        	setQueryParameter(query,map,objs);
            
        	query.setFirstResult((page-1)*size);
        	query.setMaxResults(size);
        	
        	list = query.list();
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return list;   
    }
    
    /**
     * 数据表查询返回单个对象公共函数(SQL)
     * @param openSql
     * @return
     */
	public Object openSqlOne(String openSql){
    	return openSqlOne(openSql,null,null);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(SQL)
     * @param openSql
     * @param map
     * @return
     */
	public Object openSqlOne(String openSql,Map<String,Object> map){
    	return openSqlOne(openSql,map,null);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(SQL)
     * @param openSql
     * @param objs
     * @return
     */
	public Object openSqlOne(String openSql,Object[] objs){
    	return openSqlOne(openSql,null,objs);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(SQL)
     * @param openSql
     * @param map
     * @param objs
     * @return
     */
    @SuppressWarnings("unchecked")
	private Object openSqlOne(String openSql,Map<String,Object> map,Object[] objs){
    	T t = null;
        Query query = null;
        try{
        	query = getSession().createSQLQuery(openSql);
        	
        	setQueryParameter(query,map,objs);
        	
        	t = (T) query.uniqueResult();
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return t;
    }
    
    /**
     * 数据表查询返回单个对象公共函数(HQL)
     * @param openHql
     * @return
     */
	public T openHqlOne(String openHql){
    	return openHqlOne(openHql,null,null);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(HQL)
     * @param openHql
     * @param map
     * @return
     */
	public T openHqlOne(String openHql,Map<String,Object> map){
    	return openHqlOne(openHql,map,null);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(HQL)
     * @param openHql
     * @param objs
     * @return
     */
	public T openHqlOne(String openHql,Object[] objs){
    	return openHqlOne(openHql,null,objs);
    }
    
    /**
     * 数据表查询返回单个对象公共函数(HQL)
     * @param openHql
     * @param map
     * @param objs
     * @return
     */
    @SuppressWarnings("unchecked")
	private T openHqlOne(String openHql,Map<String,Object> map,Object[] objs){
    	T t = null;
        Query query = null;
        try{
        	query = getSession().createQuery(openHql);
        	
        	setQueryParameter(query,map,objs);
        	
        	t = (T) query.uniqueResult();
        }catch (Exception e) {
			e.printStackTrace();
		}
        
        return t;   
    }
    
    /**
     * 获取查询记录数(HQL)
     * @param openHql
     * @return
     */
    public int openCountByHql(String openHql){
    	return openCountByHql(openHql, null, null);
    }
    
    /**
     * 获取查询记录数(HQL)
     * @param openHql
     * @param map
     * @return
     */
    public int openCountByHql(String openHql,Map<String,Object> map){
    	return openCountByHql(openHql, map, null);
    }
    
    /**
     * 获取查询记录数(HQL)
     * @param openHql
     * @param objs
     * @return
     */
    public int openCountByHql(String openHql,Object[] objs){
    	return openCountByHql(openHql, null,objs);
    }
    
    /**
     * 获取查询记录数(HQL)
     * @param openHql
     * @param map
     * @param objs
     * @return
     */
    private int openCountByHql(String openHql,Map<String,Object> map,Object[] objs){
    	
    	int iRet = 0;
    	 Query query = null;
         try{
         	query = getSession().createQuery(openHql);
         	
         	setQueryParameter(query,map,null);
            
         	iRet = ((Number) query.iterate().next()).intValue();
         }catch (Exception e) {
 			e.printStackTrace();
 		}
    	
    	return iRet;
    }
    
    /**
     * 获取查询记录数(SQL)
     * @param openSql
     * @return
     */
    public int openCountBySql(String openSql){
    	return openCountBySql(openSql, null, null);
    }
    
    /**
     * 获取查询记录数(SQL)
     * @param openSql
     * @param map
     * @return
     */
    public int openCountBySql(String openSql,Map<String,Object> map){
    	return openCountBySql(openSql, map, null);
    }
    
    /**
     * 获取查询记录数(SQL)
     * @param openSql
     * @param objs
     * @return
     */
    public int openCountBySql(String openSql,Object[] objs){
    	return openCountBySql(openSql, null,objs);
    }
    
    /**
     * 获取查询记录数(SQL)
     * @param openSql
     * @param map
     * @param objs
     * @return
     */
    private int openCountBySql(String openSql,Map<String,Object> map,Object[] objs){
    	
    	int iRet = 0;
    	Query query = null;
        try{
        	query = getSession().createSQLQuery(openSql);
        	
        	setQueryParameter(query,map,null);
        	
        	iRet = ((Number) query.iterate().next()).intValue();
        }catch (Exception e) {
			e.printStackTrace();
		}
    	return iRet;
    }
    
    
    /**
     * 获取查询统计字符串(HQL)
     * @param openHql
     * @return
     */
    public String openMaxByHql(String openHql){
    	return openMaxByHql(openHql, null, null);
    }
    
    /**
     * 获取查询统计字符串(HQL)
     * @param openHql
     * @param map
     * @return
     */
    public String openMaxByHql(String openHql,Map<String,Object> map){
    	return openMaxByHql(openHql, map, null);
    }
    
    /**
     * 获取查询统计字符串(HQL)
     * @param openHql
     * @param objs
     * @return
     */
    public String openMaxByHql(String openHql,Object[] objs){
    	return openMaxByHql(openHql, null,objs);
    }
    
    /**
     * 获取查询统计字符串(HQL)
     * @param openHql
     * @param map
     * @param objs
     * @return
     */
    private String openMaxByHql(String openHql,Map<String,Object> map,Object[] objs){
    	
    	 String str = "";
    	 Query query = null;
         try{
         	query = getSession().createQuery(openHql);
         	
         	setQueryParameter(query,map,null);
            
         	str = (String) query.iterate().next();
         }catch (Exception e) {
 			e.printStackTrace();
 		}
    	
    	return str;
    }
    
    /**
     * 获取查询统计字符串(SQL)
     * @param openSql
     * @return
     */
    public String openMaxBySql(String openSql){
    	return openMaxBySql(openSql, null, null);
    }
    
    /**
     * 获取查询统计字符串(SQL)
     * @param openSql
     * @param map
     * @return
     */
    public String openMaxBySql(String openSql,Map<String,Object> map){
    	return openMaxBySql(openSql, map, null);
    }
    
    /**
     * 获取查询统计字符串(SQL)
     * @param openSql
     * @param objs
     * @return
     */
    public String openMaxBySql(String openSql,Object[] objs){
    	return openMaxBySql(openSql, null,objs);
    }
    
    /**
     * 获取查询统计字符串(SQL)
     * @param openSql
     * @param map
     * @param objs
     * @return
     */
    private String openMaxBySql(String openSql,Map<String,Object> map,Object[] objs){
    	
    	String str = "";
    	Query query = null;
        try{
        	query = getSession().createSQLQuery(openSql);
        	
        	setQueryParameter(query,map,null);
        	
        	str = (String) query.iterate().next();
        }catch (Exception e) {
			e.printStackTrace();
		}
    	return str;
    }
    
    /**
     * 数据表新增、修改、删除公共函数(SQL)
     * @param execSql
     * @return
     */
    public int execSql(String execSql){
    	return execSql(execSql, null, null);
    }
    
    /**
     * 数据表新增、修改、删除公共函数(SQL)
     * @param execSql
     * @param map
     * @return
     */
    public int execSql(String execSql,Map<String,Object> map){
    	return execSql(execSql, map, null);
    }
    
    /**
     * 数据表新增、修改、删除公共函数(SQL)
     * @param execSql
     * @param objs
     * @return
     */
    public int execSql(String execSql,Object[] objs){
    	return execSql(execSql, null, objs);
    }
    
    /**
     * 数据表新增、修改、删除公共函数(SQL)
     * @param execSql
     * @param map
     * @param objs
     * @return
     */
    private int execSql(String execSql,Map<String,Object> map,Object[] objs){
    	int iRet = 0;    
        Query query = null;
        try{
	        query = getSession().createSQLQuery(execSql);
	        
	        setQueryParameter(query,map,null);
	        
	        iRet = query.executeUpdate();    
        }catch (Exception e) {
			e.printStackTrace();
		}
        return iRet; 
    }
    
    /**
     * 查询条件SQL注入参数
     * @param query
     * @param map
     * @param objs
     */
    @SuppressWarnings("unchecked")
    private void setQueryParameter(Query query,Map<String,Object> map,Object[] objs){
    	
    	if(map != null && map.size() > 0){
        	Iterator it = map.keySet().iterator();  
            while (it.hasNext())  
            {  
                Object key = it.next();  
                query.setParameter(key.toString(), map.get(key));  
            }
    	}
    	
    	if(objs != null && objs.length > 0){
    		for(int i=0;i<objs.length;i++){
    			query.setParameter(i, objs[i]);
    		}
    	}
    }
    
}
